package com.rattsoftware.client.views;

import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormButtonBinding;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HtmlEditor;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.FormPanel.LabelAlign;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.core.client.GWT;
import com.rattsoftware.client.AppMessages;
import com.rattsoftware.client.events.AppEvents;
import com.rattsoftware.client.model.FacultySummary;
import com.rattsoftware.client.util.ProgressIndicator;

public class NewEditFacultyView extends View {

	final AppMessages appMessages = GWT.create(AppMessages.class);
	
	private TextField<String> facultyName;
	private TextField<String> facultyDean;
	private HtmlEditor description;
	private TextField<String> location;
	private TextField<String> noOfDepts;
	private TextField<String> address;

	private FacultySummary facultySummary;
	private Window window;

	public NewEditFacultyView(Controller controller) {
		super(controller);
		GWT.log("NewEditFacultyView : Initialise");
	}

	@Override
	protected void handleEvent(AppEvent event) {
		GWT.log("NewEditFacultyView : handleEvent");
		if (event.getType() == AppEvents.NewFaculty) {
			createUI("Add New Faculty");
		} else if (event.getType() == AppEvents.EditFaculty) {
			facultySummary = event.getData();
			createUI("Edit Faculty Details for " + facultySummary.getFacultyName());
		}
	}
	
	private void createUI(String title) {
		window = new Window();
		window.setHeading(title);
		window.setModal(true);
		window.setPlain(true);
		window.setClosable(false);
		window.setMaximizable(false);
		window.setDraggable(true);
		window.setResizable(false);
		window.setSize(745, 495);
		window.setScrollMode(Scroll.AUTOY);
		window.setLayout(new FitLayout());

		ProgressIndicator.showProgressBar();
		window.add(getCoursePanel());
		ProgressIndicator.hideProgressBar();
		window.show();
	}
	
	private FormPanel getCoursePanel() {
		FormData formData = new FormData("100%");

		FormPanel panel = new FormPanel();
		panel.setFrame(true);
		panel.setHeaderVisible(false);
		panel.setSize(700, -1);
		panel.setLabelAlign(LabelAlign.TOP);

		LayoutContainer main = new LayoutContainer();
		main.setLayout(new ColumnLayout());

		LayoutContainer left = new LayoutContainer();
		left.setStyleAttribute("paddingRight", "10px");
		FormLayout layout = new FormLayout();
		layout.setLabelAlign(LabelAlign.TOP);
		left.setLayout(layout);

		LayoutContainer leftEditor = new LayoutContainer();
		layout = new FormLayout();
		layout.setLabelAlign(LabelAlign.TOP);
		leftEditor.setLayout(layout);

		LayoutContainer right = new LayoutContainer();
		right.setStyleAttribute("paddingLeft", "10px");
		layout = new FormLayout();
		layout.setLabelAlign(LabelAlign.TOP);
		right.setLayout(layout);

		facultyName = new TextField<String>();
		facultyName.setFieldLabel("Faculty Name");
		facultyName.setEmptyText("Name of the faculty.");

		if (facultySummary != null) {
			facultyName.setValue(facultySummary.getFacultyName());
		}

		left.add(facultyName, formData);

		facultyDean = new TextField<String>();
		facultyDean.setFieldLabel("Faculty Dean");
		facultyDean.setEmptyText("Head of the Faculty.");

		if (facultySummary != null) {
			facultyDean.setValue(facultySummary.getDeansName());
		}
		right.add(facultyDean, formData);

		address = new TextField<String>();
		address.setFieldLabel("Faculty Address");
		address.setEmptyText("Address of the faculty.");

		if (facultySummary != null) {
			address.setValue(facultySummary.getFacultyAddress());
		}
		left.add(address, formData);
		
		location = new TextField<String>();
		location.setFieldLabel("Faculty Location");
		location.setEmptyText("Location of the faculty.");

		if (facultySummary != null) {
			location.setValue(facultySummary.getLocation());
		}
		right.add(location, formData);
		
		noOfDepts = new TextField<String>();
		noOfDepts.setFieldLabel("No. Of Departments");

		if (facultySummary != null) {
			noOfDepts.setValue(facultySummary.getNumOfDepartments());
		}
		left.add(noOfDepts, formData);

		main.add(left, new ColumnData(.5));
		main.add(right, new ColumnData(.5));

		panel.add(main, new FormData("100%"));

		description = new HtmlEditor();
		description.setFieldLabel("About The Faculty");
		description.setEmptyText("This can be anything that talks about this particular faculty. " +
				"For example Services that are offered at the faculty etc");
		description.setHeight(240);

		if (facultySummary != null) {
			description.setValue(facultySummary.getFacultyDescription());
		}
		leftEditor.add(description, formData);

		panel.add(leftEditor, new FormData("100%"));

		Button buttonSave = new Button(appMessages.save());
		buttonSave.setIcon(IconHelper.createStyle("add"));
		buttonSave.addListener(Events.Select, new Listener<ButtonEvent>() {
			@Override
			public void handleEvent(ButtonEvent be) {
				// addNewCourse();
			}
		});

		Button buttonCancel = new Button(appMessages.cancel());
		buttonCancel.setIcon(IconHelper.createStyle("cancel"));
		buttonCancel.addListener(Events.Select, new Listener<ButtonEvent>() {
			@Override
			public void handleEvent(ButtonEvent be) {
				closeWindow();
			}
		});

		panel.addButton(buttonSave);
		panel.addButton(buttonCancel);

		FormButtonBinding binding = new FormButtonBinding(panel);
		binding.addButton(buttonSave);

		return panel;
	}

	@SuppressWarnings("deprecation")
	private void closeWindow() {
		facultySummary = null;
		ProgressIndicator.showProgressBar();
		window.close();
		ProgressIndicator.hideProgressBar();
	}
}